#!/usr/bin/python
# -*- coding: utf-8 -*-
from django.shortcuts import *
from django.http import *
from forms import *
from save_util import *
from models import *

def login(request):
    
    render_result = {}
    if request.method == 'GET':
        login_form = LoginForm()
    else:
        login_form = LoginForm(request.POST)
        if login_form.is_valid():
            user = User.objects.get(email = login_form.cleaned_data['email'])
            request.session['user_id'] = user.id
            return HttpResponseRedirect('/home')
			
    render_result['form'] = login_form
    return  render_to_response('login.html', render_result)

def logout(request):

    request.session.pop('user_id', None)

    return HttpResponseRedirect('/login')

def register(request):

    render_result = {}
    if request.method == 'GET':
        user_register_form = UserRegisterForm()
    else:
        user_register_form = UserRegisterForm(request.POST)
        if user_register_form.is_valid():
            user = create_new_user(user_register_form.cleaned_data)
            request.session['user'] = user
            return HttpResponseRedirect('/register/more/')
    render_result['form'] = user_register_form
    return render_to_response('register.html', render_result)

def register_more(request):

    render_result = {}
    user = request.session.get('user')
    if user == None:
        return HttpResponseRedirect('/register/')
    if request.method == 'GET':
        if user.role_type == 'S':
            role_form = StudentRoleForm()
        elif user.role_type == 'T':
            role_form = TeacherRoleForm()
    else:
        if user.role_type == 'S':
            role_form = StudentRoleForm(request.POST)
        elif user.role_type == 'T':
            role_form = TeacherRoleForm(request.POST)
        if role_form.is_valid():
            save_new_role(user, role_form)
	    request.session['user_id'] = user.id
            return HttpResponseRedirect('/home/')
    render_result['form'] = role_form
    return render_to_response('register_more.html', render_result)

def fresh_department(request):

    if request.method == 'GET':
        return HttpResponseForbidden()
    school_id = request.POST.get('school_id', '')
    if school_id == '':
        return HttpResponseNotFound()
    school_id = int(school_id)
    school = School.objects.get(id = school_id)
    return HttpResponse(get_department_options_by_school(school))
        
def show_home(request):

    #return render_to_response('show_home.html', {})
    return HttpResponseRedirect('/group/all_groups/')

def show_personal_page(request, id_or_nick):
    return render_to_response('show_personal_page.html', {})


def test(request):
    return render_to_response('base.html',{})

def wmd_test(request):
    return render_to_response('wmd_test.html', {})

def show_about(request):
    return render_to_response('about.html',{})

def show_contact(request):
    return render_to_response('contact.html',{})


def new_mail(request):

    return render_to_response('new_mail.html', {})
